package com.yztc.ui.category;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.squareup.okhttp.OkHttpClient;
import com.yztc.config.Url;
import com.yztc.helper.OkHttpClientHelper;
import com.yztc.okbuy.R;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by My on 2016/12/19.
 */

public class TitleFragment extends Fragment{
    private Context mContext=null;
    private ListView listView_title;
    private List<CartgroyBean.ResultsBean> mList=new ArrayList<>();
    private ListBaseAdapter adapter=null;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    List<CartgroyBean.ResultsBean> mList= (List<CartgroyBean.ResultsBean>) msg.obj;
                    adapter=new ListBaseAdapter(mContext,mList);
                    listView_title.setAdapter(adapter);
                    break;
            }
        }
    };
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=getActivity();
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_title,null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView_title= (ListView) view.findViewById(R.id.listView_title);
        listView_title.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        reloadNetworkData();
    }

    private void reloadNetworkData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String jsonString= OkHttpClientHelper.getStringFromURL(getActivity(),Url.CLASSIFY,1);
                    Gson gson=new Gson();
                    CartgroyBean bean=gson.fromJson(jsonString,CartgroyBean.class);
                    List<CartgroyBean.ResultsBean> list=bean.getResults();
                    mList=bean.getResults();
                    Message msg=new Message();
                    msg.what=0;
                    msg.obj=list;
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
